
#pragma once
#include "configs.h"
#include "test/socket.h"
#include <status.h>

#define TUI_CH_BTNSET_OFFSET 0x0100
#define TUI_PRE_BTNSET_OFFSET 0x00FC
#define TUI_CFG_BASEADDR 0x2000

#define TUI_Q_NUM 20
enum TUI_CH_ITEM
{
  TUI_AUX3 = 0,
  TUI_AUX4,
  TUI_AUX5,
  TUI_CH_NUM
};
enum TUI_PAGEID
{
  PAGE_NULL = -1,
  PAGE_HOME = 0,
  PAGE_CH   = 4,
  PAGE_CH_2 = 6,
  PAGE_CH_3 = 8,
  PAGE_CH_4 = 9,
  PAGE_CH_PREVIEW = 11,
  PAGE_CFG_GENERAL = 12,
  PAGE_CFG_LAN =18,
  PAGE_CFG_ACCOUNT =20,
  PAGE_CFG_ABOUT = 22,
  PAGE_CFG_LOADM =30,
  PAGE_CFG_LOADM_2 =32,
  PAGE_WAITING = 36
};

enum TUI_ADDRESSES
{
  TUI_BTN_CH1 = 0x0000,
  TUI_BTN_CH2 = 0x0002,
  TUI_BTN_CH3 = 0x0004,
  TUI_BTN_OPT = 0x0006,
  TUI_BTN_RUN = 0x0008,
  TUI_BTN_HOME = 0x0100,
  TUI_ICON_UNIT = 0x0104,
  TUI_BTN_CH1_SET = 0x0210,
  TUI_BTN_CH2_SET = 0x0220,
  TUI_BTN_CH3_SET = 0x0230,
  TUI_BTN_CH4_SET = 0x0240,
  TUI_BTN_CH5_SET = 0x0250,
  TUI_BTN_CH6_SET = 0x0260,
  TUI_BTN_CH_ON = 0x04F0,
  TUI_BTN_CH_SET_INIT = 0x0500,
  TUI_BTN_CH_SET_INITT = 0x0510,
  TUI_BTN_CH_SET_RATE1 = 0x0520,
  TUI_BTN_CH_SET_PRE1 = 0x0530,
  TUI_BTN_CH_SET_TIME1 = 0x0540,
  TUI_BTN_CH_SET_RATE2 = 0x0550,
  TUI_BTN_CH_SET_PRE2 = 0x0560,
  TUI_BTN_CH_SET_TIME2 = 0x0570,
  TUI_BTN_CH_SET_RATE3 = 0x0580,
  TUI_BTN_CH_SET_PRE3 = 0x0590,
  TUI_BTN_CH_SET_TIME3 = 0x05a0,
  TUI_BTN_CH_SET_RATE4 = 0x05b0,
  TUI_BTN_CH_SET_PRE4 = 0x05c0,
  TUI_BTN_CH_SET_TIME4 = 0x05d0,
  TUI_BTN_CH_SET_TOTALT = 0x05e0,
  TUI_TEXT_ERROR = 0x1010,
  TUI_BTN_CH_SAVE = 0x1100,
  TUI_BTN_CH_PREVIEW = 0x1102,
  TUI_BTN_CH_CANCEL = 0x1104,
  TUI_BTN_CH_PAGENUM = 0x1106,
  TUI_BTN_CH_PREVIEW_BACK = 0x1108,
  TUI_BTN_OPT_SELECT = 0x2000,
  TUI_CFG_PAGE_UPDOWN = 0x2100,
  TUI_CFG_SAVE = 0x2102,
  TUI_CFG_CANCEL = 0x2104,
  TUI_CFG_LAN_DHCP = 0x2200,
  TUI_CFG_LAN_MANUAL = 0x2202,
  TUI_CFG_LAN_SET = 0x2204,
  TUI_CFG_LAN_IP_VALUE = 0x2210,
  TUI_CFG_LAN_IP_SET = 0x2220,
  TUI_CFG_LAN_NETMASK_VALUE = 0x2230,
  TUI_CFG_LAN_NETMASK_SET = 0x2240,
  TUI_CFG_LAN_GATEWAY_VALUE = 0x2250,
  TUI_CFG_LAN_GATEWAY_SET = 0x2260,
  TUI_CFG_LAN_ADDR_END = 0x2270,
  TUI_CFG_GENERAL_UNIT = 0x211A,
  TUI_CFG_GENERAL_AUTOD = 0x212A,
  TUI_CFG_GENERAL_WAITT = 0x213A,
  TUI_CFG_GENERAL_RUN = 0x214A,
  TUI_CFG_GENERAL_AFTERRUN = 0x215A,
  TUI_CFG_GENERAL_RECVALARM = 0x216A,
  TUI_CFG_GENERAL_AFTERERROR = 0x217A,
  TUI_CFG_GENERAL_LOCKPSWD = 0x218A,
  TUI_CFG_GENERAL_HOUR = 0x2194,
  TUI_CFG_GENERAL_MINUTE = 0x219C,
  TUI_CFG_GENERAL_YEAR = 0x21A4,
  TUI_CFG_GENERAL_MONTH = 0x21BC,
  TUI_CFG_GENERAL_DAY = 0x21B4,
  TUI_CFG_LOADM_ADDR_START = 0x2300,
  TUI_CFG_LOADM_ADDR_END = 0x23F0,
  TUI_CFG_ACCOUNT_START,
  TUI_CFG_ACCOUNT_PSWD1 = 0x2400,
  TUI_CFG_ACCOUNT_PSWD2 = 0x2404,
  TUI_CFG_ACCOUNT_DEBUG = 0x2408,
  TUI_CFG_ACCOUNT_END,
  TUI_BTN_LOCK_PASSWORD = 0x240C,
};
enum CONFIG_ITEM_ID
{
  TUI_CFG_NONE = 0x00,
  TUI_CFG_GENERAL,
  TUI_CFG_LAN,
  TUI_CFG_WIFI,
  TUI_CFG_RS232,
  TUI_CFG_ACCOUNT,
  TUI_CFG_THEME,
  TUI_CFG_LOADM,
  TUI_CFG_ABOUT
};


enum TUI_CFG_GENERAL_INDEX
{
  TUI_CFG_ALL  = 0x06,
  TUI_CFG_UNIT = 0x1A,
  TUI_CFG_AUTOD = 0x2A,
  TUI_CFG_WAITT = 0x3A,
  TUI_CFG_START = 0x4A,
  TUI_CFG_AFTER_RUN = 0x5A,
  TUI_CFG_RECV_ALARM = 0x6A,
  TUI_CFG_AFTER_ERROR = 0x7A,
  TUI_CFG_LOCK_PSWD = 0x8A,
  TUI_CFG_TIME_HOUR = 0x94,
  TUI_CFG_TIME_MINUTE = 0x9C,
  TUI_CFG_DATE_YEAR = 0xA4,
  TUI_CFG_DATE_MONTH = 0xBC,
  TUI_CFG_DATE_DAY = 0xB4
};
enum TUI_LOAD_METHOD_STEP
{
  TUI_LOAD_METHOD_NULL = 0,
  TUI_LOAD_METHOD_SET_ON,
  TUI_LOAD_METHOD_WAIT_ON
};
enum TUI_UNIT_ITEM
{
  TUI_UNIT_KPA = 0,
  TUI_UNIT_PSI,
  TUI_UNIT_MPA,
  TUI_UNIT_BAR,
  TUI_UNIT_NUM
};
extern long unitScale[TUI_UNIT_NUM];

enum TUI_DATA_TYPE
{
  TUI_DATA_PRESSURE = 0,
  TUI_DATA_OTHER
};
enum TUI_RUN_STATE
{
  TUI_RUN_STOP = 0,
  TUI_RUN_START
};
typedef struct MSG82_SEND
{
  unsigned char header[2];
  unsigned char length;
  unsigned char cmd;
  unsigned char addr[2];
  unsigned char data[100];
}MSG82_SEND;
typedef struct MSG80_SEND
{
  unsigned char header[2];
  unsigned char length;
  unsigned char cmd;
  unsigned char addr;
  unsigned char data[100];
}MSG80_SEND;

typedef struct TUI_METHOD_ENTRY
{
  unsigned short IDStart;
  unsigned short IDRange;
  void (*operate)(unsigned char* buffer,int num,int staticParam);
  int staticParam;
}TUI_METHOD_ENTRY;

typedef struct TUIStatus
{
  char readyState;//0x1000:0-red,1-yellow,2-green  
  unsigned int waitTime;//the time waiting for user control
  char runState;  //0x0012:0-stop,1-run
  char CHItem;    //the Item of now CH
  char nowPage;
  short temperature;
  unsigned char cfgStep;
  long PasswordNew1;
  long PasswordNew2;
  unsigned char MethodPreview;
  CHStatus CHs[6];
}TUIStatus;

typedef struct MethodStatus
{
  unsigned char MethodSelected;
  unsigned char MethodStep;  
  unsigned char isCHset[6];
}MethodStatus;

extern USER_CONFIG_DATA m_TUIConfig;
extern TUIStatus m_TUIStatus;
extern TUIStatus m_TUIlastStatus;
extern TUIStatus m_TUIStatusBackup;

extern MSG82_SEND TUI_defMsg82();
extern MSG80_SEND TUI_defMsg80();
extern int TUITask();
extern int TUI_openUART(char *Dev);
extern int TUI_init();
extern int TUI_sendMsg(unsigned char* buffer,int num);
extern int TUI_updateMethodStatus();//this func is used for load the preset method to the config now.
extern int TUI_appendCmd(unsigned char* buffer,int buff_length);
extern int TUI_appendAns(unsigned char* buffer,int buff_length);
extern int TUI_readCmd(unsigned char* buffer,int buff_length);
extern int TUI_readAns(unsigned char* buffer,int buff_length);
